#include <bits/stdc++.h>
// 2025/01/22
// tag: 
// Author: Zhang Muen
using namespace std;

int parents[20001], n, m, k;
int dp[20001];
// 并查集
class UF
{
public:
    UF(int n)
    {
        for (int i = 1; i <= n; i++)
            parents[i] = i;
    };

    int find(int i)
    {
        if (parents[i] != i)
            return parents[i] = find(parents[i]);
        return parents[i];
    }

    void un(int i, int j)
    {
        int ri = find(i), rj = find(j);
        if (ri == rj)
            return;
        parents[ri] = rj;
    }
};

unordered_map<int, int> mp;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k;
    UF uf(n);
    for (int i = 1; i <= k; i++){
        int a, b;
        cin >> a >> b;
        uf.un(a, b);
    }
    for (int i = 1; i <= n; i++)
        mp[uf.find(i)]++;
    for (auto i : mp){
        for (int j = n; j >= i.second; j--)
            dp[j] = max(dp[j], dp[j - i.second] + i.second);
    }
    int ans = INT_MAX, a = 0;
    for (int i = 1; i <= n; i++)
        if (abs(m - dp[i]) < ans)
            ans = abs(m - dp[i]), a = dp[i];
    if (ans >= m){
        cout << 0;
        return 0;
    }
    cout << a;
    return 0;
}